<!-- 项目审批 -->
<template>
	<view>
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">出库明细编辑</block></cu-custom>
	<scroll-view style="height: 100%" scroll-y="true">
		<form @submit="onSubmit">
			<view class="cu-form-group">
				<view class="title">名称</view>
				<input v-model="item.inStockGoodsDetail.goodsName" placeholder="" name="input" @focus="selectGoods()"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">规格</view>
				<input v-model="item.inStockGoodsDetail.spec" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">产地</view>
				<input v-model="item.inStockGoodsDetail.maker" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">型号</view>
				<input v-model="item.inStockGoodsDetail.model" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">材质</view>
				<input v-model="item.inStockGoodsDetail.materialCode" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">批次</view>
				<input v-model="item.batchNum" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">货位</view>
				<input v-model="item.goodsPosi" placeholder="" name="input"></input>
			</view>
	        <view class="cu-form-group">
	        	<view class="title">包装类型</view>
				<input v-model="item.inStockGoodsDetail.packType" placeholder="" name="input"></input>
	        </view>
			<view class="cu-form-group">
				<view class="title">入库计重方式</view>
				<input v-model="item.inStockGoodsDetail.weightMethod" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">库存数量</view>
				<input class="flex-item-20" v-model="item.inStockGoodsDetail.qty" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">库存重量</view>
				<input v-model="item.inStockGoodsDetail.weight" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">冻结数量</view>
				<input v-model="item.inStockGoodsDetail.frzQty" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">冻结重量</view>
				<input v-model="item.inStockGoodsDetail.frzWeight" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">应发数量</view>
				<input v-model="item.qty" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">应发重量</view>
				<input v-model="item.weight" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">实发数量</view>
				<input v-model="item.actualQty" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">实发重量</view>
				<input v-model="item.actualWeight" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view class="cu-form-group">
				<!-- <view class="title">作业班组</view> -->
				<uni-combox label="作业班组" :candidates="workGroupOptions" placeholder="请选择作业班组" v-model="item.workGroup"></uni-combox>
			</view>
			<view class="cu-form-group">
				<!-- <view class="title">设备</view> -->
				<uni-combox label="设备" :candidates="machineOptions" placeholder="请选择设备" v-model="item.machine"></uni-combox>
			</view>
			<view class="cu-form-group">
				<uni-combox label="出库方式" :candidates="goodsOutTypeOptions" placeholder="请选择出库方式" v-model="item.goodsOutType"></uni-combox>
			</view>
			<view class="cu-form-group">
				<!-- <view class="title">计重方式</view> -->
				<uni-combox label="计重方式" :candidates="weightMethodOptions" placeholder="请选择计重方式" v-model="item.weightMethod"></uni-combox>
			</view>
			<view class="cu-form-group">
				<view class="title">保管员</view>
				<input v-model="item.tallyClerk" placeholder="输入保管员" name="input"></input>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">提货单号</view>
				<input v-model="item.goodsOutOrdCode" placeholder="输入提货单号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<uni-combox label="单据类型" :candidates="ordTypeOptions" placeholder="请选择单据类型" v-model="item.ordType"></uni-combox>
			</view>
			<view class="cu-form-group">
				<view class="title">车牌号</view>
				<input v-model="item.vehicleNum" placeholder="输入车牌号" name="input"></input>
			</view> -->
			<view>
				<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
					<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
			</view>
		</form>
		<!-- <view class="cu-modal" :class="isShowGoodsDialog ? 'show' : ''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">选择货物</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view style="height: 450px; overflow: auto;">
					<select-goods style="height: 440px;" />
				</view>
			</view>
		</view> -->
		<view class="cu-modal drawer-modal justify-start" :class="isShowGoodsDialog ? 'show' : ''" @tap="hideModal">
			<view class="cu-dialog basis-lg" @tap.stop="" >
				<select-goods style="height: 95%" :goodsName="item.inStockGoodsDetail.goodsName" @selected="selectedGoods" />
			</view>
		</view>
	</scroll-view>
	</view>
	
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'
	import selectGoods from './selectGoods.vue'
	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox,
			selectGoods
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: { inStockGoodsDetail: {goodsName:''} },
				detailId: 0,
				goodsOutBillId:0,
				weightMethodOptions:['抄码','过地磅','过电子磅','理算','平均件重'],
				workGroupOptions:[],
				machineOptions:[],
				goodsOutTypeOptions:['','吊装','人工自装'],
				ordTypeOptions: ['客户提单','物资调拨单','电子订单'],
				data: [],
				defaultProps: {
					children: 'children',
					label: 'fullName'
				},
				isShowGoodsDialog: false,
				loading: false
			}
		},
		onLoad(options) {

			this.detailId = options.detailId
			this.goodsOutBillId = options.goodsOutBillId
			if (this.detailId >0 && this.detailId > 0) {
				this.$minApi.goodsOutBill(this.goodsOutBillId).then(res => {
					const data = res
					if (data) {
						const detailId = Number(this.detailId)
						const detail =  data.goodsOutDetails.find(f => f.id === detailId)

						if (detail) {
							this.item = detail
						}
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			} else {
				uni.setNavigationBarTitle({
				title: '新增出库明细'
				})
			}
						
			this.$minApi.getWorkGroups().then(res => {
				const data = res
				if (data) {
					this.workGroupOptions =	data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})
			
			this.$minApi.getMachines().then(res => {
				const data = res
				if (data) {
					this.machineOptions = data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})

		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  
				
				this.loading = true
				const _this = this
				if (this.detailId > 0) {
					this.item.qty = Number(this.item.qty)
					this.item.weight = Number(this.item.weight)
					this.item.actualQty = Number(this.item.actualQty)
					this.item.actualWeight = Number(this.item.actualWeight)
					this.item.goodsOutBillId =  Number(this.goodsOutBillId)
					
					_this.$minApi.updateGoodsOutDetail(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsOutBill/detail?id=${this.goodsOutBillId}`
							})
						}, 2000)
				
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
					}).finally(() => {
						this.loading = false
					})
				} else {
					
					this.item.qty = Number(this.item.qty)
					this.item.weight = Number(this.item.weight)
					this.item.actualQty = Number(this.item.actualQty)
					this.item.actualWeight = Number(this.item.actualWeight)
					this.item.goodsOutBillId =  Number(this.goodsOutBillId)
					
					// this.item.goodsOutDetailID = Number(this.item.goodsOutDetailID)
					_this.$minApi.createGoodsOutDetail(this.item).then(res => {
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsOutBill/detail?id=${this.goodsOutBillId}`
							})
						}, 2000)
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '添加失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			hideModal(e) {
				this.isShowGoodsDialog = false
			},
			selectGoods() {	
				this.isShowGoodsDialog = true
			},
			selectedGoods(val) {
				this.isShowGoodsDialog = false
				if (val) {
					
					if (val) {
						
						this.item.inStockGoodsID = val.goodsID
						this.item.goodsPosi = val.goodsPosi
						this.item.batchNum = val.batchNum						
						this.item.actualQty = 0
						this.item.actualWeight = 0
						this.item.qty = 0
						this.item.weight = 0
						this.item.isInputTaskQuantity='否'
						this.item.isPayGoodsInFee='否'
						this.item.isPayStockFee='否'
						this.item.oldName=''
						
						this.item.inStockGoodsDetail.goodsName = val.goodsName						
						this.item.inStockGoodsDetail.qtyUnit = val.qtyUnit
						this.item.inStockGoodsDetail.weightUnit = val.weightUnit						
						this.item.inStockGoodsDetail.spec = val.spec
						this.item.inStockGoodsDetail.model = val.model
						this.item.inStockGoodsDetail.materialCode = val.materialCode
						this.item.inStockGoodsDetail.maker = val.maker
						this.item.inStockGoodsDetail.frzQty = val.frzQty
						this.item.inStockGoodsDetail.frzWeight = val.frzWeight						
						
					}
				}
				
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/goodsOutBill/detail`
				})
			}
		}
	}
</script>

<style>

</style>
